package com.zboot.zbootadmin.project.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zboot.zbootadmin.common.utils.PagingResult;
import com.zboot.zbootadmin.project.base.command.machinery.MachineryModelListCommand;
import com.zboot.zbootadmin.project.base.entity.MachineryCategory;
import com.zboot.zbootadmin.project.base.entity.MachineryModel;
import com.zboot.zbootadmin.project.base.mappers.MachineryModelMapper;
import com.zboot.zbootadmin.project.base.service.MachineryModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author draw
 * @since 2021-05-08
 */
@Service
public class MachineryModelServiceImpl extends ServiceImpl<MachineryModelMapper, MachineryModel> implements MachineryModelService {
@Autowired
private MachineryModelMapper machineryModelMapper;
    @Override
    public void updateToDelete(Long id) {
        machineryModelMapper.updateToDelete(id);
    }

    @Override
    public PagingResult pageList(MachineryModelListCommand command) {
        Page pageVo = new Page(command.getPage(), command.getPageSize());
        LambdaQueryWrapper<MachineryModel> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(command.getName())) {
            queryWrapper.like(MachineryModel::getName, command.getName());
        }

        if (Objects.nonNull(command.getStatus())) {
            queryWrapper.eq(MachineryModel::getStatus, command.getStatus());
        }
        queryWrapper.orderByDesc(MachineryModel::getCreateDate);
        IPage<MachineryModel> iPage = machineryModelMapper.selectPage(pageVo, queryWrapper);
        PagingResult pagingResult= new PagingResult();
        pagingResult.setItems(iPage.getRecords());
        pagingResult.setTotalCount(iPage.getTotal());
        return pagingResult;
    }
}
